package com.wwdx.service.impl;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wwdx.model.WwdxBookChapterModel;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.model.WwdxThemeModel;
import com.wwdx.model.dto.ZhiNiaoBookDTO;
import com.wwdx.service.WwdxBookServiceI;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author liguoliang
 */
@Service
public class WwdxBookServiceImpl extends BaseServiceImpl<WwdxBookModel> implements WwdxBookServiceI{

    @Override
    public Long selectModelCount(WwdxBookModel model){
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(6);
        String hql = "select count(1) from WwdxBookModel\t"+getParams(model,params);
        return count(hql,params);
    }

    private String getParams(WwdxBookModel model, Map<String, Object> params){
        StringBuilder sb = new StringBuilder("where 1=1\n");
        String orderBy = "\torder by gmtCreated desc";
        //书籍ID查询
        Long bookid = model.getBookid();
        if (bookid != null){
            sb.append("and bookid=:bookid\n");
            params.put("bookid",bookid);
        }
        //书籍名称查询
        String bookname = model.getBookname();
        if (StringUtils.isNotBlank(bookname)){
            sb.append("and bookname like :bookname\n");
            params.put("bookname","%"+bookname+"%");
        }
        //是否上线查询
        Integer status = model.getStatus();
        if (status != null){
            sb.append("and status=:status\n");
            params.put("status",status);
        }
        //撰稿人或音频转述师下的书籍查询
        Long userid = model.getUserid();
        if (userid != null){
            sb.append("and (videoauthorid=:userid or copywriter=:userid)\n");
            params.put("userid",userid);
        }
        //书籍关键字查询
        String word = model.getWord();
        if (StringUtils.isNotBlank(word)){
            sb.append("and (bookname like :word or bookauthor like :word)\n");
            params.put("word","%"+word+"%");
        }
        //是否首页显示查询
        Integer ishost = model.getIshost();
        if (ishost != null){
            orderBy = "\tORDER BY sort desc";
            params.put("ishost",ishost);
            sb.append("and ishost=:ishost\t");
        }
        //书籍类型查询
        Integer type = model.getType();
        if (type != null){
            params.put("type",type);
            sb.append("and type=:type\t");
        }
        //最热排序查询
        Integer type1 = model.getType1();
        if(type1 != null && type1 == 2){
            orderBy = "\tORDER BY reference_click_count desc";
        }
        //主题下的书籍查询
        Long themeid = model.getThemeid();
        if (themeid != null){
            WwdxThemeModel wwdxThemeModel = wwdxThemeService.getByThemeId(themeid);
            String bookList = wwdxThemeModel.getBook_list();
            Iterable<String> iterable = Splitter.on(",").omitEmptyStrings().trimResults().split(bookList);
            List<Long> bookids = Lists.newArrayListWithExpectedSize(16);
            iterable.forEach(id -> bookids.add(Long.valueOf(id)));
            params.put("ids",bookids);
            sb.append("and bookid IN (:ids)\n");
        }
        //随机获取书籍数查询
        Boolean random = model.isRandom();
        if (random){
            orderBy = "\torder by rand()";
        }
        sb.append(orderBy);
        return sb.toString();
    }

    @Override
    public List<WwdxBookModel> selectModelList(WwdxBookModel model,Integer page,Integer rows){
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(6);
        String hql = "from WwdxBookModel\t"+getParams(model,params);
        return find(hql,params,page,rows);
    }

    @Override
    public void bookStatistics(Long bookid) {
        WwdxBookModel wwdxBookModel = this.getByBookId(bookid);
        Long clickCount = wwdxBookModel.getClick_count();
        Long referenceClickCount = wwdxBookModel.getReference_click_count();
        if (clickCount == null){
            clickCount = 0L;
        }
        if (referenceClickCount == null){
            referenceClickCount = 1000L;
        }
        clickCount ++;
        referenceClickCount += RandomUtils.nextInt(1, 4);
        wwdxBookModel.setClick_count(clickCount);
        wwdxBookModel.setReference_click_count(referenceClickCount);
        wwdxBookModel.setGmtModified(new Date());
        update(wwdxBookModel);
    }

    @Override
    public WwdxBookModel getByBookId(Long bookid){
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("bookid",bookid);
        return getByHql("from WwdxBookModel where bookid=:bookid", params);
    }

    @Override
    public List<ZhiNiaoBookDTO> selectBookList(WwdxBookModel model, Integer page, Integer rows) {
        List<Integer> ids = Lists.newArrayList(
                9571, 13032, 85766, 216539, 13051, 2378, 5327, 99599, 7449, 2445, 216473, 134564, 159412, 48783, 71676, 118381, 171155, 171156, 171157, 171158, 171159, 171160, 124667, 214316, 1504249504, 13047, 1504253430, 1504601515, 190174, 2273, 1504344543, 1504419817,
                1504411479, 1504408007, 10901, 208304, 12988, 1504424817, 1504426925, 110722, 55927, 1968, 12932, 217221, 3150, 13011, 217536, 87666, 141925, 2574, 99591, 77083, 5665, 9058, 131462, 1504349969, 1504441850, 1504451409, 1504447134, 217943, 2602, 95256, 12743, 116166, 81674,
                200675, 1504510401, 1504506194, 2287, 1504496955, 1504492719, 6119, 9401, 6107, 1504513333, 8619, 1504502552, 1504506016, 216840, 1504493173, 1504508705, 1504511779, 1504506569, 213994, 1504515694, 157164, 217822, 157164, 480, 159957, 216864, 150, 216542, 10987, 1504690297,
                1504693113, 83366, 89654, 11149, 13044, 2341, 3321, 109609, 159964, 155980, 1504516002, 218092, 1504266502, 1504263935, 1504251272, 1504259760, 5327, 1504325472, 6135, 9553, 216539, 208811, 1505294625, 1508490256, 1505286650, 1514971394, 1505290265, 1510035457,
                1513927907, 1507615104, 1507527335, 1506671969, 1505706029, 1506498980, 1507616266, 1511252459, 1505892931, 1507891348, 1512631943, 1506756023, 1506591216, 1507879383, 1506308828, 1516868948, 1507538856, 1505897378, 1509678242, 1511860215, 1512722717, 1505715237, 1506418630,
                1515036701, 1514194633, 1513232368, 1515394849, 1515403279, 1528966733, 1512549695, 1512026778, 1511430794, 1514183435, 1515478515, 1527155096, 1529378336, 1532678838, 1533115280, 1508740277, 1532684073, 153267590, 1517988445, 1513150064, 1514535117, 1515554540, 1514861863, 1520842693,
                1524206665, 1524711606, 1525417753, 1529053373, 1532677670, 1532659335, 1532659691, 1532659807, 1533003286, 1533003901, 1534152390, 1534153081, 1534154606, 1536648929, 1536649204, 1536649469, 1515554540, 1512546024, 1516952797, 1514872175, 1514188287, 1512713913, 1524120037, 1524120212,
                1528885054, 1512030335, 1534414896, 1516934782, 1513761392, 1517301858, 1521799099, 1521792773, 1512615887, 1525413689, 1529044876, 1533028846, 1532671729, 1534237401, 1513923503, 1516259711, 1515649771, 1513236148, 1516330867, 1516938129, 1516872009, 1516954951, 1516865934, 1516957471,
                1516947522, 1516951505, 1518243246, 1516959738, 1520825721, 1521706565, 1524046612, 1524212824, 1524217561, 1524216227, 1524729891, 1525337604, 1534229069, 1512719642, 1515652961, 1514451515, 1512727820, 1516011883, 1515661966, 1514442599, 1515657284, 1516181834, 1512959068, 1516862779,
                1513579061, 1516183376, 1517205084, 1513306040, 1505284476, 1515661966, 1516255158, 1517207592, 1517295245, 1517296529, 1524193671, 1517282136, 1517195852, 1517298779, 1517193053, 1517291910, 1520840739, 1517280030, 1517191891, 1520839005, 1520837138, 1521618172, 1521794662, 1524711252,
                1524711418, 1524129020, 1524129125, 1524203801, 1528883381, 1528883544, 1527839684, 1529050516, 1532076984, 1532661490, 1532679737, 1532078550, 1534414245, 1532079292, 1533112889, 1533112997, 1533113138, 1534235245, 1512122077, 1520822721, 1521700463, 1521789627, 1524208663, 1525416732,
                1529051589, 1534155187, 1520820946, 1520833727, 1521614441, 1521708740, 1528958102, 1525341614, 1527841783, 1527841783, 1529057369, 1529057448, 1529057634, 1528451653, 1529032312, 1529046291, 1529056241, 1532678327, 1534230696, 1536745892, 1524107112, 1524039728, 1524036565, 1524214241, 1528452992, 1525342827, 1533889799, 1533115793,
                1533032541, 1533114257, 1532080539, 1534225261, 1524626380, 1528965986, 1529055445, 1529388820, 1533030712, 1527838098, 1527148687, 1533112048, 1534234147, 1528452332, 1529054626, 1521710438, 1534412846, 1529391371, 1528967981, 1528961335, 1532676893, 1532674461, 1529389901, 1529029657, 1529029657, 1532680401, 1532681501, 1533031587
        );
        List<Integer> ids2 = ids.subList(0, 9);
        List<ZhiNiaoBookDTO> dtos = Lists.newArrayListWithExpectedSize(ids2.size());
        for (Integer id : ids2) {
            String hql = "select new WwdxBookModel(bookid,bookname,bookrecommend,bookauthor,bookimg) from WwdxBookModel where bookid ="+id;
            WwdxBookModel book = getByHql(hql);
            String hql2 = "select new WwdxBookChapterModel(chapterid,chaptername,chapterduration,chaptersize,chaptervideo) from WwdxBookChapterModel where bookid ="+id;
            List<WwdxBookChapterModel> chapterList = wwdxBookChapterService.find(hql2);
            List<ZhiNiaoBookDTO.ChapterListDTO> zhiNiaoBookDTOs = Lists.newArrayListWithExpectedSize(chapterList.size());
            chapterList.forEach(chapter -> {
                zhiNiaoBookDTOs.add(
                        ZhiNiaoBookDTO.ChapterListDTO.builder().id(chapter.getChapterid()).name(chapter.getChaptername()).duration(chapter.getChapterduration())
                                .size(chapter.getChaptersize()).url(chapter.getChaptervideo()).build()
                );
            });
            dtos.add(
                    ZhiNiaoBookDTO.builder().srcId(book.getBookid()).name(book.getBookname()).desc(book.getBookrecommend()).developers(book.getBookauthor())
                            .smallImg(book.getBookimg()).coursewares(zhiNiaoBookDTOs).build()
            );
        }
        return dtos;
    }
}














